package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.mapper.DtsPermissionMapper;
import com.qf.pojo.DtsPermission;
import com.qf.service.DtsPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ZhangYuJie
 * projectName:shop_manage
 * time:2022/5/16 16:58
 * description:
 */
@Service
public class DtsPermissionServiceImpl implements DtsPermissionService {
    @Autowired
    DtsPermissionMapper permissionMapper;

    LambdaQueryWrapper<DtsPermission> qw = new LambdaQueryWrapper<>();
    @Override
    public Set<String> findPermByIds(Integer[] roleIds) {
        qw.in(DtsPermission::getRoleId, roleIds);

        List<DtsPermission> permissionList = permissionMapper.selectList(qw);


        Set<String> permissions = permissionList.stream().map(permission -> {
            return permission.getPermission();
        }).collect(Collectors.toSet());

        return permissions;
    }

    @Override
    public Set<String> queryByRoleIds(Integer[] roleIds) {
        Set<String> permissions = new HashSet<String>();

        //判断是否有角色参数
        if (roleIds == null || roleIds.length == 0) {
            return permissions;
        }

        //查询数据库
        QueryWrapper<DtsPermission> wapper = new QueryWrapper<>();
        wapper.in("role_id", Arrays.asList(roleIds));
        wapper.eq("deleted", false);
        List<DtsPermission> permissionList = permissionMapper.selectList(wapper);

        for (DtsPermission permission : permissionList) {
            permissions.add(permission.getPermission());
        }

        return permissions;
    }

    @Override
    public boolean checkSuperPermission(Integer roleId) {
        if (roleId == null) {
            return false;
        }
        QueryWrapper<DtsPermission> wapper = new QueryWrapper<>();
        wapper.eq("role_id", roleId);
        wapper.eq("permission", "*");
        wapper.eq("deleted", false);
        Integer count = permissionMapper.selectCount(wapper);

        return count != 0;
    }

    @Override
    public Set<String> queryByRoleId(Integer roleId) {
        Set<String> permissions = new HashSet<String>();
        if (roleId == null) {
            return permissions;
        }

        //查询数据库
        QueryWrapper<DtsPermission> wapper = new QueryWrapper<>();
        wapper.eq("role_id", roleId);
        wapper.eq("deleted", false);
        List<DtsPermission> permissionList = permissionMapper.selectList(wapper);

        for (DtsPermission permission : permissionList) {
            permissions.add(permission.getPermission());
        }

        return permissions;
    }

    @Override
    public void deleteByRoleId(Integer roleId) {
        QueryWrapper<DtsPermission> wapper = new QueryWrapper<>();
        wapper.eq("role_id", roleId);
        wapper.eq("deleted", false);
        permissionMapper.delete(wapper);
    }

    @Override
    public void add(DtsPermission dtsPermission) {
        dtsPermission.setAddTime(new Date());
        dtsPermission.setUpdateTime(new Date());
        permissionMapper.insert(dtsPermission);
    }
}
